package com.example.myapplication;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by anker on 2020/9/16.
 *
 * @author nina.ma
 */
public class UpdateFwCircleView extends View {
    private Paint circlePaint, bgPaint;
    private int width, height;
    //attrs
    private int startColor;
    private int endColor;
    private int bgColor;
    private float nextAngle = 0;
    //animation
    private ValueAnimator animator;

    public UpdateFwCircleView(Context context) {
        super(context);
        initPaint();
    }

    public UpdateFwCircleView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.UpdateFwCircleView,
                0, 0);
        startColor = array.getColor(R.styleable.UpdateFwCircleView_start_color, Color.parseColor(
                "#53D6FF"));
        endColor = array.getColor(R.styleable.UpdateFwCircleView_end_color, Color.parseColor(
                "#329DFF"));
        bgColor = array.getColor(R.styleable.UpdateFwCircleView_bg_color,
                getResources().getColor(R.color.colorSpecialNormal));
        array.recycle();
        initPaint();
    }

    private void initPaint() {
        circlePaint = new Paint();
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setDither(true);
        circlePaint.setAntiAlias(true);
        circlePaint.setStrokeWidth(10f);

        bgPaint = new Paint();
        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setDither(true);
        bgPaint.setAntiAlias(true);
        bgPaint.setStrokeWidth(10f);
        bgPaint.setColor(bgColor);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        @SuppressLint("DrawAllocation")
        LinearGradient gradient = new LinearGradient(0, 0, width, height, startColor, endColor,
                Shader.TileMode.MIRROR);
        circlePaint.setShader(gradient);
        canvas.drawArc(0, 0, width, height, 0, 360, false, bgPaint);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc(0, 0, width, height, -90, nextAngle, true, circlePaint);
        }
    }

    int current = 0;
    TimerTask timerTask;
    Timer timer;
    int period;
    int total;
    IUpdateFwCircleView listener;
    int second = 0;
    long currentTime= 0l;

    public void initTask() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (System.currentTimeMillis() - currentTime >= 1000) {
                    currentTime = System.currentTimeMillis();
                    second++;
                }
                if (current < 360) {
                    current += 1;
                    nextAngle = current;
                    postInvalidate();
                    listener.animatorUpdate(current, second);
                } else {
                    stopAnimation();
                    listener.animatorTimeEnd();
                }
            }
        };
    }


    public void startAnimation(IUpdateFwCircleView listener, int animateTime) {
        if (timer != null || listener == null || animateTime < 1) {
            return;
        }
        this.listener = listener;
        total = animateTime * 1000;//360度总时长
        period = total / 360; //每一度耗费时长
        initTask();
        timer = new Timer(true);
        timer.schedule(timerTask, 0, period);
    }

    public void stopAnimation() {
        if (this.timerTask != null) {
            this.timerTask.cancel();
        }
        if (this.timer != null) {
            this.timer.cancel();
        }
        this.timerTask = null;
        this.timer = null;
        this.total = 0;
        this.period = 0;
        this.current = 0;
        this.nextAngle = 0;
        this.second = 0;
        postInvalidate();
    }

    public void pause() {
        if (this.timerTask != null) {
            this.timerTask.cancel();
        }
        currentTime = System.currentTimeMillis();
    }

    public void resume() {
        if (this.timer != null) {
            initTask();
            timer.schedule(timerTask, 0, period);
        }
        currentTime = System.currentTimeMillis();
    }

    public interface IUpdateFwCircleView {
        void animatorUpdate(int process, int second);

        void animatorTimeEnd();
    }
}
